package com.zzyl.nursing.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author zheng
 * @date 2025-01-02
 */
@Transactional
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        LambdaQueryWrapper<NursingProjectPlan> lqw= new LambdaQueryWrapper<>();
        lqw.eq(NursingProjectPlan::getPlanId, id);

        List<NursingProjectPlan> list = nursingProjectPlanMapper.selectList(lqw);
        List<NursingProjectPlanVo> npp = new ArrayList<>() {};
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                NursingProjectPlanVo nppv = new NursingProjectPlanVo();
                BeanUtils.copyProperties(list.get(i), nppv);
                npp.add(nppv);
            }
        }

//        BeanUtils.copyProperties(list, npp);
        System.out.println("=====================================");
        System.out.println(list);
        System.out.println(npp);
        System.out.println("=====================================");
        nursingPlanVo.setProjectPlans(npp);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectList(null);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */

    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insert(nursingPlan);

        int count = nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
        return count == 0 ? 0 : 1;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto dto) {
        try {
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(dto, nursingPlan);

            nursingProjectPlanMapper.deleteByPlanId(dto.getId());

            if (dto.getProjectPlans() != null && dto.getProjectPlans().size() > 0) {
                nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), dto.getId());
            }

            return nursingPlanMapper.updateById(nursingPlan);
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量删除护理计划
     *
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {

        try {
            nursingProjectPlanMapper.deleteById(id);
            return nursingPlanMapper.deleteNursingPlanByIds(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<NursingPlanVo> selectAll() {
        List<NursingPlanVo> list = nursingPlanMapper.selectAll();
        return list;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
//    @Override
//    public int deleteNursingPlanById(Integer id) {
//        nursingProjectPlanMapper.deleteByPlanId(id);
//        return nursingPlanMapper.deleteNursingPlanById(id);
//    }
}
